package com.ebt.m.proposal_v2.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

/**
 * @author archer.qi
 *         Created on 2017/6/23.
 */
public class MaxWebView extends WebView {

    private int mMaxWidth;
    private int mMaxHeight;

    public MaxWebView(Context context) {
        super(context);
    }

    public MaxWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setMaxWidth(int maxWidth) {
        mMaxWidth = maxWidth;
    }

    public void setMaxHeight(int maxHeight) {
        mMaxHeight = maxHeight;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (mMaxWidth > 0 && getMeasuredWidth() > mMaxWidth) {
            if (mMaxHeight > 0 && getMeasuredHeight() > mMaxHeight) {
                setMeasuredDimension(mMaxWidth, mMaxHeight);
            } else {
                setMeasuredDimension(mMaxWidth, getMeasuredHeight());
            }
        } else {
            if (mMaxHeight > 0 && getMeasuredHeight() > mMaxHeight) {
                setMeasuredDimension(getMeasuredWidth(), mMaxHeight);
            } else {
                setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
            }
        }
    }
}
